package com.feihao.zhou.service.system.Impl;

import com.feihao.zhou.bean.system.User;
import com.feihao.zhou.factory.MapperFactory;
import com.feihao.zhou.mapper.system.UserMapper;
import com.feihao.zhou.service.system.UserService;
import com.feihao.zhou.utils.MD5Util;
import com.feihao.zhou.utils.TransactionUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;

public class UserServiceImpl implements UserService {

    @Override
    public void save(User user) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            UserMapper mapper = MapperFactory.getMapper(sqlSession, UserMapper.class);
            user.setId(UUID.randomUUID().toString());

            //密码加密：
            String password = user.getPassword();
            user.setPassword(new MD5Util().md5(password));

            mapper.save(user);
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            //有异常的声明出来：
            e.printStackTrace();
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        } finally {
            TransactionUtil.close(sqlSession);
        }

    }

    @Override
    public void delete(String id) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            UserMapper mapper = MapperFactory.getMapper(sqlSession, UserMapper.class);
            mapper.delete(id);
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            //有异常的声明出来：
            e.printStackTrace();
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public void update(User user) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            UserMapper mapper = MapperFactory.getMapper(sqlSession, UserMapper.class);
            mapper.update(user);
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            //有异常的声明出来：
            e.printStackTrace();
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public User findById(String id) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            UserMapper mapper = MapperFactory.getMapper(sqlSession, UserMapper.class);
            User user = mapper.findById(id);
            TransactionUtil.commit(sqlSession);
            return user;
        } catch (Exception e) {
            //有异常的声明出来：
            e.printStackTrace();
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public List<User> findAll() {
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            UserMapper mapper = MapperFactory.getMapper(sqlSession, UserMapper.class);
            List<User> all = mapper.findAll();

            System.out.println(all);
            TransactionUtil.commit(sqlSession);
            return all;
        } catch (Exception e) {
            //有异常的声明出来：
            e.printStackTrace();
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public PageInfo<User> findAll(Integer page, Integer size) {
        SqlSession sqlSession = null;
        try {
            sqlSession = MapperFactory.getSqlSession();
            UserMapper mapper = MapperFactory.getMapper(sqlSession, UserMapper.class);
            PageHelper.startPage(page,size);

            List<User> all = mapper.findAll();

            PageInfo pageInfo = new PageInfo(all);
            TransactionUtil.commit(sqlSession);
            return pageInfo;
        } catch (Exception e) {
            //有异常的声明出来：
            e.printStackTrace();
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }
}
